package cn.wolfcode.ss.controller;

import cn.wolfcode.ss.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private AuthenticationManager authenticationManagerBean;

    @PostMapping("/jwt/login")
    public String login(String uname,String pwd){
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(uname, pwd);
        // 在spring security规则下，自定义执行登录校验逻辑
        Authentication authenticate = authenticationManagerBean.authenticate(authenticationToken);
        if (authenticate != null && authenticate.isAuthenticated()){
            return JWTUtils.createToken("username", uname);
        }
        throw new RuntimeException("账号或者密码错误");
    }
}
